// pages/Work-content/Work-content.js
// var bookList = require('../data/data.js');
import * as API from "../../utils/config.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
  
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    //后台数据获取
    this._youngmanDatas();
    var postId = options.id;
    this.data.currentPostId = postId;
    console.log(postId);
    // var bookList = this.data.bookList[postId];
    // this.data.youngmanLists = youngmanLists
    // 点击收藏
    var postsCollected = wx.getStorageSync('posts_Collected')
    if (postsCollected){
      var postCollected = postsCollected[postId]
      this.setData({
        collected: postCollected
      })
    }else{
      var postsCollected={};
      postsCollected[postId]=false;
      wx.setStorageSync('posts_Collected', postsCollected)
    }

  },
  // 点击收藏事件
  onCollectionTap:function(event){
    var postsCollected = wx.getStorageSync('posts_Collected');
    var postCollected=postsCollected[this.data.currentPostId];
    postCollected = !postCollected;
    postsCollected[this.data.currentPostId] = postCollected;
    // this.showModal(postsCollected, postCollected);
    this.showToast(postsCollected, postCollected);

    // // 更新文章是否收藏的缓存值
    // wx.setStorageSync('posts_Collected', postsCollected);
    // // 更新数据绑定变量，从而实现切换图片
    // this.setData({
    //   collected:postCollected
    // })
    //弹框
    // wx.showToast({
    //   title: postCollected?"收藏成功":"取消成功",
    //   // 弹框的消失时长
    //   duration:1000,
    //   //icon只能是success或者loading
    //   icon:"success"

    // })
    //点击确认
   
    

  },
  //end
  //点击确认
  showModal: function (postsCollected,postCollected){
    var that=this;
  wx.showModal({
    title: '收藏',
    content: postCollected?'收藏该文章？':'取消收藏该文章',
    showCancel: true,
    cancelText: '取消',
    cancelColor: '#333',
    confirmText: '确认',
    confirmColor: '#405f80',
    success: function (res) {
        if(res.confirm){
          // 更新文章是否收藏的缓存值
          wx.setStorageSync('posts_Collected', postsCollected);
          // 更新数据绑定变量，从而实现切换图片
          that.setData({
            collected: postCollected
          })
        }
     },
  })
},
//点击确认end
// 弹框式收藏
  showToast: function (postsCollected, postCollected){
    var that=this;
    // 更新文章是否收藏的缓存值
    wx.setStorageSync('posts_Collected', postsCollected);
    // 更新数据绑定变量，从而实现切换图片
    that.setData({
      collected: postCollected
    })
    wx.showToast({

      title: postCollected?"收藏成功":"取消成功",
    //   // 弹框的消失时长
      duration:1000,
    //   //icon只能是success或者loading
      icon:"success"
     

    })
  },
//弹框式收藏end
// 点击分享
  onshareTap:function(event){
      var itemList=[
      "分享给微信好友",
      "分享到朋友圈",
      "分享到QQ",
      "分享到微博"

      ]
      wx.showActionSheet({
        itemList: itemList,
        itemColor:"#405f80",
        success:function(res){
          //res.cancel用户是不是点击了取消按钮
          //res.tapIndex数组元素的序号，从0开始
          wx.showModal({
            title: "用户"+itemList[res.tapIndex],
            content: "用户是否取消？" + res.cancel+"现在无法实现分享功能",
          })
        }
      })
  },
// 点击分享end
  //end
  _youngmanDatas() {
    wx.request({
      url: API.youngman,
      success: (res) => {
        this.setData({          
          youngmanLists: res.data.result.bookList,
        })
        // console.log(this.data.youngmanLists)
      
      }
    })
    // console.log(API)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})